package javamusic.DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;


import javamusic.excepcion.DAOExcepcion;
import javamusic.modelo.Cliente;
import javamusic.modelo.Local;
import javamusic.modelo.Reserva;
//import javamusic.modelo.Reserva;
import javamusic.modelo.ReservaCliente;

import javamusic.util.ConexionBDKaraoke;


public class ReservaClienteDAO extends BaseDAO{
		
public ReservaCliente insertar(ReservaCliente vo) throws DAOExcepcion {
	
	//String query1 = "select idcliente from cliente where correo=?";	
	String query = "insert into reserva (idreserva,fecha,descripcion,elog,idsala,idcliente) values (?,?,?,?,?,(select idcliente from cliente where correo=?))";
			
		
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		
		try {
			con = ConexionBDKaraoke.obtenerConexion();
			//stmt = con.prepareStatement(query1);
			stmt = con.prepareStatement(query);
			stmt.setString(1, vo.getFechas());
			stmt.setString(2, vo.getDescripcion());
			stmt.setInt(3, vo.getElog());
			stmt.setInt(4, vo.getIdsala());
			stmt.setString(5,vo.getCorreo());
			
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo insertar");
			}
			// Obtener el ultimo id
			
			vo.setElog(0);
			int id  = 0;
			query = "select last_insert_id()";
			stmt = con.prepareStatement(query);
			rs = stmt.executeQuery();
			if (rs.next()) {
				id = rs.getInt(1);
				
			}
			vo.setIdreserva(id);

		} catch (SQLException e) {
			//System.err.println(e.getMessage());
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		
		return vo;
	}
	
	
	
	
	/*
	 * LOGIN DE CLIENTE
	 */
	public Collection<ReservaCliente> Login(String correo,String clave) throws DAOExcepcion{
		
		String query = "select  correo,clave  from cliente where  correo = ?";
		Collection<ReservaCliente> pro = new ArrayList<ReservaCliente>();
		Connection con =null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
				
		try {
			con = ConexionBDKaraoke.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, correo );
			rs =stmt.executeQuery();
			while (rs.next()){
				ReservaCliente vo = new ReservaCliente();
				
				vo.setCorreo(rs.getString("correo"));
				vo.setClave(rs.getNString("clave"));
				pro.add(vo);			}
			
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
			}finally{
				this.cerrarResultSet(rs);
				this.cerrarStatement(stmt);
				this.cerrarConexion(con);
			}
			return pro;
		}

	/*
	 * 
	 */
	
	public Collection<ReservaCliente> buscaPorNombre(String nombrelocal) throws DAOExcepcion{
		String query = "select  sala.nombre,sala.capacidad,sala.descripcion,local.nombre  from sala , local where sala.idlocal= local.idlocal and local.nombre =?";
		Collection<ReservaCliente> pro = new ArrayList<ReservaCliente>();
		Connection con =null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBDKaraoke.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, nombrelocal );
			rs =stmt.executeQuery();
			while (rs.next()){
				ReservaCliente vo = new ReservaCliente();
				
				vo.setNombresala(rs.getString("sala.nombre"));
				vo.setCapacidad(rs.getInt("sala.capacidad"));
				vo.setDescripcion(rs.getString("sala.descripcion"));
				vo.setNombrelocal(rs.getString("local.nombre"));
				
				pro.add(vo);
			}
			
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
			}finally{
				this.cerrarResultSet(rs);
				this.cerrarStatement(stmt);
				this.cerrarConexion(con);
			}
			return pro;
		}
	
	
	
	
	/*
	 * 
	 */
	
	
	public Collection<ReservaCliente> listar() throws DAOExcepcion {
		Collection<ReservaCliente> c = new ArrayList<ReservaCliente>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBDKaraoke.obtenerConexion();
			String query = "select nombre from local";
			stmt = con.prepareStatement(query);
			rs = stmt.executeQuery();
			while (rs.next()) {
				ReservaCliente vo = new ReservaCliente();
				
				vo.setNombre(rs.getString("nombre"));
			
				c.add(vo);
			}

		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return c;
	}

	
	
	

	
	

}



